package com.system.user.service.impl;

import com.system.user.dao.*;
import com.system.user.entity.LightingEntity;
import com.system.user.entityVO.WatchVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.system.user.entity.UserEntity;
import com.system.user.service.UserService;
import org.springframework.transaction.annotation.Transactional;


@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private BindDao bindDao;

    @Autowired
    private WaterDao waterDao;

    @Autowired
    private LightingDao lightingDao;

    @Autowired
    private GasDao gasDao;

    @Override
    public List<UserEntity> findAll() {
        return userDao.selectUser();
    }

    @Override
    public List<String> findOpenidByBNumber(String bNumber) {
        return userDao.selectOpenIDByBNumber(bNumber);
    }

    @Override
    public boolean adminRegister(String username, String password) {
        UserEntity user = userDao.selectUserByUsername(username);
        if (user == null) {
            int is_success = userDao.adminRegister(username, password);
            if (is_success > 0) {
                return true;
            }
        }
        return false;
    }

    @Override
    public UserEntity adminLogin(String username, String password) {
        UserEntity user = userDao.adminLogin(username, password);
        return user;
    }

    @Override
    public UserEntity weiXinLogin(String openid) {
        UserEntity user = userDao.weiXinLogin(openid);
        if (user == null) {
            String username = "未设置";
            int is_success = userDao.weiXinRegister(username, openid);
            if (is_success > 0) {
                user = userDao.weiXinLogin(openid);
            }
        }
        return user;
    }

    @Override
    public List<WatchVO> queryAllWatch(Integer uid) {
        List<WatchVO> list = new ArrayList<>();
        List<WatchVO> watchVOS = waterDao.queryWaterByUid(uid);
        List<WatchVO> lightingVOS = lightingDao.queryWatchByUid(uid);
        List<WatchVO> gasVOS = gasDao.queryWatchByUid(uid);
        if (watchVOS.size() > 0) {
            list.addAll(watchVOS);
        }
        if (lightingVOS.size() > 0) {
            list.addAll(lightingVOS);
        }
        if (gasVOS.size() > 0) {
            list.addAll(gasVOS);
        }
        return list;
    }

    @Override
    public List<WatchVO> findOtherWatch(WatchVO watchVO) {
        List<WatchVO> list = new ArrayList<>();
        List<WatchVO> watchVOS = waterDao.queryOtherWatch(watchVO);
        List<WatchVO> lightingVOS = lightingDao.queryOtherWatch(watchVO);
        List<WatchVO> gasVOS = gasDao.queryOtherWatch(watchVO);
        if (watchVOS.size() > 0) {
            list.addAll(watchVOS);
        }
        if (lightingVOS.size() > 0) {
            list.addAll(lightingVOS);
        }
        if (gasVOS.size() > 0) {
            list.addAll(gasVOS);
        }
        return list;
    }

    @Override
    public boolean updateUser(UserEntity userEntity) {
        if (userDao.updateUser(userEntity) > 0) {
            return true;
        }
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteUser(Integer id) {
        if (userDao.deleteUser(id) > 0) {
            bindDao.deleteBindByUid(id);
            return true;
        }
        return false;
    }

}